package com.buba.config;

import com.buba.system.pojo.SystemMenu;
import com.buba.system.pojo.SystemUserRole;
import com.buba.system.pojo.SystemUsers;
import com.buba.system.service.RoleService;
import com.buba.system.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.HashSet;
import java.util.Set;

/**
 * @description: 自定义领域
 * @Author: 游王子
 * @date: 2021/8/20 16:27
 */
public class CustomRealm extends AuthorizingRealm {

    private UserService userService;
    private RoleService roleService;
    /**
     * 方法描述 授权
     *
     * @param principalCollection
     * @author:
     * @return: org.apache.shiro.authz.AuthorizationInfo
     * @date: 2021/8/23
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //1.先获取到用户名
        String account = (String) principalCollection.getPrimaryPrincipal();
        SystemUsers systemUsers=new SystemUsers();
        systemUsers.setUserName(account);
        //2.从数据库或者缓存中获取角色数据
        SystemUserRole users = roleService.selSystem(systemUsers);
        //根据角色名获取角色权限
        Set<String> systemMenuSet=roleService.selSystemMenu(users.getRole_Name());
       // 创建授权类
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
       // 向授权类添加用户拥有的角色和权限
        simpleAuthorizationInfo.setStringPermissions(systemMenuSet);
        simpleAuthorizationInfo.addRole(users.getRole_Name());
        return simpleAuthorizationInfo;
    }

    /**
     * 方法描述 认证（也就是登录执行的方法）
     *
     * @param authenticationToken
     * @author:
     * @return: org.apache.shiro.authc.AuthenticationInfo
     * @date: 2021/8/23
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        // 获取到登录用户的帐号
        String userName = (String) authenticationToken.getPrincipal();
        if (userName == null || "".equals(userName.trim())) return null;
        // 通过帐号查询用户信息
        SystemUsers systemUsers = new SystemUsers();
        systemUsers.setUserName(userName);
        SystemUsers login = userService.login(systemUsers);
        if (login.getUserPassword() == null) return null;
        // 这个是shiro主要进行认证的类
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(userName, login.getUserPassword(), "customRealm");
        // 如果密码经过salt加密，需要在这里设置salt值。
        simpleAuthenticationInfo.setCredentialsSalt(ByteSource.Util.bytes(login.getUserName()));
        return simpleAuthenticationInfo;
    }
    @Autowired
    @Qualifier("RoleServiceImpl")
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    @Autowired
    @Qualifier("UserServiceImpl")
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
